package com.ruoyi.api.controller;

import com.ruoyi.api.domin.Student;
import com.ruoyi.api.common.AppController;
import com.ruoyi.framework.interceptor.annotation.IgnoreAuth;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/mongo")
public class MongoController extends AppController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoOperations mongoOperations;

    /*增加*/
    @RequestMapping("/insert")
    @IgnoreAuth
    public Object insert(Student student){
        mongoTemplate.insert(student);
        return writeResultRep();
    }

    /*修改*/
    @RequestMapping("/update")
    @IgnoreAuth
    public Object update(Student student) {
        //修改的条件
        Query query = new Query(Criteria.where("studentId").is(student.getStudentId()));
        //修改的内容
        Update update = new Update();
        update.set("name",student.getName());
        mongoTemplate.updateFirst(query,update,Student.class);
        return writeResultRep();
    }

    /*查询全部*/
    @RequestMapping("/findAll")
    @IgnoreAuth
    public Object findAll(Integer pageNum,Integer pageSize) {
        Query query = new Query(Criteria.where("age").is("13"));
        // 分页
        query.skip((pageNum - 1) * pageSize);
        query.limit(pageSize);
        //排序
        query.with(new Sort(Sort.Direction.DESC, "studentId"));
        //查询总记录数
        int count = (int) mongoTemplate.count(query, Student.class);
        System.out.println("数据总数"+count);
        List<Student> items = mongoTemplate.find(query, Student.class);

        int totalPage = count % pageSize == 0 ? 1 : count / pageSize + 1;
        Map<Object, Object> map = new HashMap<>();
        map.put("totalNum",count);
        map.put("totalPage",totalPage);
        map.put("items",items);
        return map;
    }

    /*删除*/
    @RequestMapping("/delete/{studentId}")
    @IgnoreAuth
    public Object delete(@PathVariable("studentId") String studentId) {
        Student student = mongoTemplate.findById(studentId,Student.class);
        mongoTemplate.remove(student);
        return writeResultRep();
    }

}
